﻿using System;

namespace ZilLion.Service.Core.Snowflake
{
    public static class System
    {
        public static Func<long> currentTimeFunc = InternalCurrentTimeMillis;
 
        public static long CurrentTimeMillis()
        {
            return currentTimeFunc();
        }

        //未使用
        //public static IDisposable StubCurrentTime(Func<long> func)
        //{
        //    currentTimeFunc = func;
        //    return new DisposableAction(() =>
        //    {
        //        currentTimeFunc = InternalCurrentTimeMillis;
        //    });  
        //}

        //未使用
        //public static IDisposable StubCurrentTime(long millis)
        //{
        //    currentTimeFunc = () => millis;
        //    return new DisposableAction(() =>
        //    {
        //        currentTimeFunc = InternalCurrentTimeMillis;
        //    });
        //}

        private static readonly DateTime Jan1St2000 = new 
           DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        private static long InternalCurrentTimeMillis()
        {
            //每500ms一个新的时间戳
            long time = (long)(DateTime.UtcNow - Jan1St2000).TotalMilliseconds;
            return time / 500;

        }        
    }
}
